import 'dart:io';
import 'package:flutter/material.dart';

enum SIFontWeight {
  light,
  regular,
  bold,
}

class SIFont {
  final String name;       // 字体名称
  final Map<SIFontWeight, String>? variants; // 添加字体变体支持
  final bool isBuiltIn;    // 是否为内置字体
  final String? path;       // 字体文件路径
  double _size;    // 改为私有变量

  SIFont({
    required this.name,
    this.variants,
    this.isBuiltIn = false,
    this.path,
  }) : _size = 0;
    
  // 修改 size getter
  double get size {
    if (path != null && !isBuiltIn) {
      try {
        final file = File(path!);
        final bytes = file.lengthSync();
        _size = bytes / 1024.0 / 1024.0; // 转换为 MB
        return _size;
      } catch (e) {
        print('获取文件大小错误: $e');  // 添加错误日志
        return _size;
      }
    }
    return _size;
  }

  // 辅助方法获取特定权重的字体
  String? getVariant(SIFontWeight weight) => variants?[weight];
  
  // 获取字体权重的显示文本
  static String weightToString(SIFontWeight weight) {
    switch (weight) {
      case SIFontWeight.light: return 'Light';
      case SIFontWeight.regular: return 'Regular';
      case SIFontWeight.bold: return 'Bold';
    }
  }

  SIFont copyWith({
    String? name,
    Map<SIFontWeight, String>? variants,
    double? size,
  }) {
    return SIFont(
      name: name ?? this.name,
      variants: variants ?? this.variants,
      isBuiltIn: isBuiltIn ?? this.isBuiltIn,
      path: path ?? this.path,
    );
  }

  // 添加一个方法来获取字体权重
  static FontWeight getFontWeight(String fontName) {
    final parts = fontName.split(' ');
    if (parts.length < 2) return FontWeight.normal;
    
    switch (parts.last.toLowerCase()) {
      case 'thin': return FontWeight.w100;
      case 'extralight': return FontWeight.w200;
      case 'light': return FontWeight.w300;
      case 'regular': return FontWeight.normal;
      case 'medium': return FontWeight.w500;
      case 'semibold': return FontWeight.w600;
      case 'bold': return FontWeight.bold;
      case 'extrabold': return FontWeight.w800;
      case 'black': return FontWeight.w900;
      default: return FontWeight.normal;
    }
  }
} 
